import os
import pandas as pd
import shutil
import zipfile

# xls文件目录
xls_path = '..\\data\\REPACKAGE_label.xls'
# apk-origin目录
origin_apk_path = '..\\data\\origin\\'
# apk-repackage目录
repackage_apk_path = ''
# 清理后repack目录
repack_path = '..\\data\\repack-unzip\\'

df_data = pd.read_excel(xls_path, sheet_name='15297个重打包，2776个原始apk')
# print(df_data['SHA256_ORIGINAL'])

apk_list = os.listdir(origin_apk_path)

# 数据集完整时，直接分类
for index, apk_info in df_data.iterrows():
    apk_origin_sha = apk_info['SHA256_ORIGINAL']
    apk_repackage_sha = apk_info['SHA256_REPACKAGE']
    opath = os.path.join(repack_path, apk_origin_sha)
    if os.path.exists(opath):
        print("已存在目录:" + opath)
        print("解压对应重打包文件" + apk_repackage_sha)
        # 解压对应的class.dex到源apk目录
        refn = os.path.join(repackage_apk_path, apk_repackage_sha + ".apk")
        refile = zipfile.ZipFile(refn)
        refile.extract("classes.dex", opath + "\\" + apk_repackage_sha)
        refile.close()
    else:
        print("不存在目录:" + opath)
        print("解压对应重源文件和重打包文件")
        os.mkdir(opath)
        # 目录不存在就解压源apk和重打包apk的classes.dex
        orfn = os.path.join(origin_apk_path, apk_origin_sha + ".apk")
        orfile = zipfile.ZipFile(orfn)
        orfile.extract("classes.dex", opath)
        orfile.close()
        refn = os.path.join(repackage_apk_path, apk_repackage_sha + ".apk")
        refile = zipfile.ZipFile(refn)
        refile.extract("classes.dex", opath + "\\" + apk_repackage_sha)
        refile.close()


# 遍历df，返回索引和SHA列表
# 数据集不完整时:
# for index, apk_info in df_data.iterrows():
#     apk_sha = apk_info['SHA256_ORIGINAL']
#     for root, dirs, files in os.walk(repack_path):
#         for file in files:
#             path = os.path.join(root, file)
#             # 寻找是否已创建目录
#             if fnmatch(path, "*" + apk_sha + "*"):
#                 # 寻找行对应重打包apk
#                 apk_sha_re = apk_info['SHA256_REPACKAGE']
#                 for apk_file in apk_list:
#                     # 寻找是否存在该apk
#                     apk_fn = os.path.splitext(apk_file)[0]
#                     if apk_sha_re == apk_fn:
#                         # 如果存在就复制到相应目录
#                         filepath = os.path.join(origin_apk_path, apk_file)
#                         shutil.copy(filepath, path)
#                         origin_apk_ex = True
#                     else:
#                         print("未能找到对应重打包apk:" + apk_sha_re + ".apk")
#
#             # 如果没有目录
#             else:
#                 for apk_file in apk_list:
#                     apk_fn = os.path.splitext(apk_file)[0]
#                     # 查找原始APK是否存在
#                     if apk_sha == apk_fn:
#                         filepath = os.path.join(origin_apk_path, apk_file)
#                         move_to_repath = repack_path + str(index)
#                         os.mkdir(repack_path + move_to_repath)
#                         shutil.copy(filepath, repack_path + move_to_repath)
#                         # 寻找对应行中重打包文件
#                         apk_sha_re = apk_info['SHA256_REPACKAGE']
#                         apk_fn_ = os.path.splitext(apk_file)[0]
#                         for apk_file_ in apk_list:
#                             if apk_sha_re == apk_fn_:
#                                 # 如果存在就复制到相应目录
#                                 filepath = os.path.join(origin_apk_path, apk_file_)
#                                 shutil.copy(filepath, move_to_repath)
#                             else:
#                                 print("源apk存在，未能找到对应重打包apk:" + apk_sha_re + ".apk")
#                     else:
#                         print("未能找到对应原始apk:" + apk_sha + ".apk")
